<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/17
 * Time: 17:02
 */

namespace Home\Controller;


use Think\Controller;

class SliderController extends CommonController
{
    //@todo 加载轮播图列表
    public function index(){
        $list=D('banner')->select();
        $this->assign('banner_list',$list);
        $this->display();
    }

    //@todo 图片上传动作
    public function UploadFile(){
        $import=new \Think\Upload(C('BannerConfig'));//实例化上传类
        $info=$import->upload();//图片上传操作
        if(!$info){
            $this->ajaxReturn(array('success' =>'error'));
        }
        //生成缩略图
        $thumb= new \Think\Image();
        $pic=$info[0]['savepath'].$info[0]['savename'];
        $uploadSrc=C('BannerConfig')['rootPath'].$pic;
        $thumb->open($uploadSrc);
        $data = date('Ymd',time());
        $thumb->thumb(200, 200)->save(C('BannerConfig')['rootPath'].$info[0]['savepath'].'thumb_'.$info[0]['savename']);
        //把图片地址添加到数据库
        $banner=D('banner');
        $data=array();
        $data['pic']=$pic;
        $data['thumb']=$info[0]['savepath'].'thumb_'.$info[0]['savename'];
        $data['ac_time']=time();
        $id=$banner->add($data);
        if (!$id) {
            $this->ajaxReturn(array('success' =>'error'));
        }
        $this->ajaxReturn(array('success' =>'success'));
    }
    //@todo 删除操作
    public function del_Action()
    {
        $id=$_GET['id'];
        $info=D('banner')->find($id);
        $thumb='./Uploads/Cms/Banner/'.$info['thumb'];
        $pic='./Uploads/Cms/Banner/'.$info['pic'];
        if (is_file($thumb) && is_file($pic)) {
            unlink($thumb);
            unlink($pic);
            $del=D('banner')->delete($id);
            if(!$del){
                $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
            }
            $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
        }
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败'));
    }
}
